/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fw.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

/**
 * 
 * @author A0125
 */
public class VelocityUtil {

    private static final String CHARSET_UTF8 = "UTF-8";
    private String templateName;
    private String exportFileFullPath;

    public VelocityUtil(String templateName) {
	this.templateName = templateName;
    }

    public VelocityUtil(String templateName, String fullPath) {
	this.templateName = templateName;
	this.exportFileFullPath = fullPath;
    }

    public String getExportFileFullPath() {
	return exportFileFullPath;
    }

    public void setExportFileFullPath(String exportFileFullPath) {
	this.exportFileFullPath = exportFileFullPath;
    }

    public String getTemplateName() {
	return templateName;
    }

    public void setTemplateName(String templateName) {
	this.templateName = templateName;
    }

    public boolean generateReportFile(VelocityContext context) {
	try {
	    Template template = null;
	    try {
		template = Velocity.getTemplate(this.templateName, VelocityUtil.CHARSET_UTF8);
	    } catch (ResourceNotFoundException rnfe) {
		rnfe.printStackTrace();
	    } catch (ParseErrorException pee) {
		pee.printStackTrace();
	    }

	    File f = new File(this.exportFileFullPath);
	    FileOutputStream fos = new FileOutputStream(f);
	    Writer writer = new BufferedWriter(new OutputStreamWriter(fos, VelocityUtil.CHARSET_UTF8));
	    try {
		if (template != null) {
		    template.merge(context, writer);
		    return true;
		} else {
		    return false;
		}
	    } catch (Exception e) {
		e.printStackTrace();
		return false;
	    } finally {
		writer.flush();
		fos.flush();
		writer.close();
		fos.close();
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return false;
	}
    }

    public String getReportAsString(VelocityContext context) {
	try {
	    Template template = null;
	    try {
		template = Velocity.getTemplate(this.templateName, VelocityUtil.CHARSET_UTF8);
	    } catch (ResourceNotFoundException rnfe) {
		rnfe.printStackTrace();
	    } catch (ParseErrorException pee) {
		pee.printStackTrace();
	    }
	    StringWriter sw = new StringWriter();
	    try {
		if (template != null) {
		    template.merge(context, sw);
		    return sw.getBuffer().toString();
		} else {
		    return null;
		}
	    } catch (Exception e) {
		e.printStackTrace();
		return null;
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
    }
}
